﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_PhuongPhapTKH;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_KeKhai;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_KhauHao;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh
{
    public partial class TSCD_DieuChinh_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {
        List<Control> controls = new List<Control>();
        #region ISupportNavigation Members
        System.Nullable<int> ThoiGianSuDungMoiHienThoi = null;
        int SoLanKhauHao = 0;
        double TongTienKhauHao = 0;
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "DieuChinh"; }
        }

        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public TSCD_DieuChinh_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public TSCD_DieuChinh_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_DieuChinh_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_DieuChinh_MoreInformation_GroupBox, controls);
           
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource TSCD_DieuChinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_DieuChinh_ViewModelSource"];
                if (Khoa == null)
                {
                    TSCD_DieuChinh_ViewModel TSCD_DieuChinh_ViewModel = new TSCD_DieuChinh_ViewModel();
                    TSCD_DieuChinh_ViewModel.ChildList_AddedNew += new TSCD_DieuChinh_ViewModel.ChildList_AddedNew_EventHandler(TSCD_DieuChinh_ViewModel_ChildList_AddedNew);                                      
                    TSCD_DieuChinh_ViewModel.tscdIdChanged += new EventHandler(TSCD_DieuChinh_TSCDIdChanged);
                    TSCD_DieuChinh_ViewModel.NguyenGiaMoiChanged += new EventHandler(TSCD_DieuChinh_NguyenGiaMoiChanged);
                    TSCD_DieuChinh_ViewModel.ThoiGianSuDungMoiChanged += new EventHandler(TSCD_DieuChinh_ThoiGianSuDungMoiChanged);
                    TSCD_DieuChinh_CollectionViewSource.Source = new List<object> { TSCD_DieuChinh_ViewModel };

                    
                }
                else
                {
                    TSCD_DieuChinh_ViewModel TSCD_DieuChinh_ViewModel = new TSCD_DieuChinh_ViewModel((int)Khoa);
                    TSCD_DieuChinh_ViewModel.ChildList_AddedNew += new TSCD_DieuChinh_ViewModel.ChildList_AddedNew_EventHandler(TSCD_DieuChinh_ViewModel_ChildList_AddedNew);
                    TSCD_DieuChinh_ViewModel.tscdIdChanged += new EventHandler(TSCD_DieuChinh_TSCDIdChanged);
                    TSCD_DieuChinh_ViewModel.NguyenGiaMoiChanged += new EventHandler(TSCD_DieuChinh_NguyenGiaMoiChanged);
                    TSCD_DieuChinh_ViewModel.ThoiGianSuDungMoiChanged += new EventHandler(TSCD_DieuChinh_ThoiGianSuDungMoiChanged);
                    TSCD_DieuChinh_CollectionViewSource.Source = new List<object> { TSCD_DieuChinh_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_PhuongPhapTKH_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongPhapTKH_ListViewModelSource"];
                DM_PhuongPhapTKH_ListViewModel dm_PhuongPhapTKH_ListViewModel = new DM_PhuongPhapTKH_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_PhuongPhapTKH_CollectionViewSource.Source = new List<object> { dm_PhuongPhapTKH_ListViewModel };


                System.Windows.Data.CollectionViewSource tscd_KhauHao_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KhauHao_ListViewModelSource"];
                TSCD_KhauHao_ListViewModel tscd_KhauHao_ListViewModel = new TSCD_KhauHao_ListViewModel(BusinessConfiguration.Configuration.KhauHaoFilterMode);
                tscd_KhauHao_CollectionViewSource.Source = new List<object> { tscd_KhauHao_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };


                System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
                TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = new TSCD_KeKhai_ListViewModel(BusinessConfiguration.Configuration.KhauHaoFilterMode);
                tscd_KeKhai_CollectionViewSource.Source = new List<object> { tscd_KeKhai_ListViewModel };

                this.TSCD_DieuChinh_DieuChinhChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_DieuChinh_DieuChinhChiTietGridView);
               
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_DieuChinh_DieuChinhChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_DieuChinh_DieuChinhChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                
            }
        }

        #endregion
       

        #region Event Handle

        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TSCD_DieuChinh_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.TSCD_DieuChinh_DieuChinhChiTietTab.IsSelected = true;
                }
                

            }
            
            e.Handled = true;
        }


        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.TSCD_DieuChinh_DieuChinhChiTietTab.IsSelected)
            {
                AddDieuChinhDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.TSCD_DieuChinh_DieuChinhChiTietTab.IsSelected)
            {
                return TSCD_DieuChinh_DieuChinhChiTietGridView;
            }
           
           
            return null;
        }



        void DeleteItems()
        {
            if (this.TSCD_DieuChinh_DieuChinhChiTietTab.IsSelected)
            {
                TSCD_DieuChinh_ChiTietChild ChiTietChild = (TSCD_DieuChinh_ChiTietChild)(this.TSCD_DieuChinh_DieuChinhChiTietGridView.SelectedItem);
                IList<TSCD_DieuChinh_ChiTietChild> list = GetViewModel().Model.TSCD_DieuChinh_ChiTiets;
                int index = list.IndexOf(ChiTietChild);
                
                DeleteDieuChinhDetailsItem(index);
            }
           
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           TSCD_DieuChinh_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddDieuChinhDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddDieuChinhDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddDieuChinhDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }

        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddDieuChinhDetailsItem();
        }

        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            List<TabItem> AvailableTabItem = new List<TabItem>();
            int indexCurrentTab = 0;
            foreach (TabItem item in this.tabControl.Items)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    AvailableTabItem.Add(item);
                    if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                    {
                        indexCurrentTab = AvailableTabItem.IndexOf(item);
                    }

                }
            }


            TabItem[] list = AvailableTabItem.ToArray();
            if (indexCurrentTab == (AvailableTabItem.Count - 1))
            {
                this.tabControl.SelectedItem = list[0];
                FocusAfterChangeTab(this.TSCD_DieuChinh_DieuChinhChiTietGridView, e.Index);

            }
           

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
            
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        private void TSCD_DieuChinh_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        

       
        #endregion

        #region View Model Related Methods
        private TSCD_DieuChinh_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TSCD_DieuChinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_DieuChinh_ViewModelSource"];
            return ((List<object>)TSCD_DieuChinh_CollectionViewSource.Source)[0] as TSCD_DieuChinh_ViewModel;
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
        }

       
        void TSCD_DieuChinh_NguyenGiaMoiChanged(object sender, EventArgs e)
        {

            System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)tscd_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();

            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.TSCDId)
                {
                    GetViewModel().Model.ChenhLechNguyenGia = Math.Abs((float)(GetViewModel().Model.NguyenGiaMoi - item.NguyenGia)) ;
                    GetViewModel().Model.GiaTriConLaiMoi = GetViewModel().Model.NguyenGiaMoi - TongTienKhauHao;
                    if (GetViewModel().Model.NguyenGiaMoi > item.NguyenGia)
                    {
                        int index = 0;
                        foreach (TSCD_DieuChinh_ChiTietChild childItem in GetViewModel().Model.TSCD_DieuChinh_ChiTiets)
                        {
                            if (childItem.DienGiai.Equals("Điều chỉnh giảm nguyên giá TSCĐ: " + item.Ma))
                            {
                                this.DeleteDieuChinhDetailsItem(index);
                                break;
                            }
                            index++;
                        }
                        this.AddDieuChinhDetailsItem();
                        GetViewModel().FinishAddNew += new EventHandler(OnFinishAddNewWhenNguyenGiaChanged);
                        
                        break;
                    }
                    else if (GetViewModel().Model.NguyenGiaMoi < item.NguyenGia)
                    {
                        int index = 0;
                        foreach (TSCD_DieuChinh_ChiTietChild childItem in GetViewModel().Model.TSCD_DieuChinh_ChiTiets)
                        {
                            if (childItem.DienGiai.Equals("Điều chỉnh tăng nguyên giá TSCĐ: " + item.Ma))
                            {
                                this.DeleteDieuChinhDetailsItem(index);
                                break;
                            }
                            index++;
                        }
                        this.AddDieuChinhDetailsItem();
                        GetViewModel().FinishAddNew += new EventHandler(OnFinishAddNewWhenNguyenGiaChanged);

                        break;
                    }
                    else
                    {
                        int index = 0;
                        foreach (TSCD_DieuChinh_ChiTietChild childItem in GetViewModel().Model.TSCD_DieuChinh_ChiTiets)
                        {

                            if (childItem.DienGiai.Equals("Điều chỉnh tăng nguyên giá TSCĐ: " + item.Ma) || childItem.DienGiai.Equals("Điều chỉnh giảm nguyên giá TSCĐ: " + item.Ma))
                            {
                                this.DeleteDieuChinhDetailsItem(index);
                                break;
                            }
                            index++;
                        }
                    }
  
                }
            }
           
        }

        void OnFinishAddNewWhenNguyenGiaChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)tscd_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();

            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.TSCDId)
                {
                    if (GetViewModel().Model.TSCD_DieuChinh_ChiTiets.Count != 0)
                    {
                        if (GetViewModel().Model.NguyenGiaMoi > item.NguyenGia)
                        {
                            TSCD_DieuChinh_ChiTietChild ChiTietChild = GetViewModel().Model.TSCD_DieuChinh_ChiTiets[GetViewModel().Model.TSCD_DieuChinh_ChiTiets.Count - 1];
                            ChiTietChild.DienGiai = "Điều chỉnh tăng nguyên giá TSCĐ: " + item.Ma;
                            ChiTietChild.TKNoId = (int)item.TkNguyenGiaId;
                            ChiTietChild.SoTien = GetViewModel().Model.ChenhLechNguyenGia;
                        }
                        else if (GetViewModel().Model.NguyenGiaMoi < item.NguyenGia)
                        {
                            TSCD_DieuChinh_ChiTietChild ChiTietChild = GetViewModel().Model.TSCD_DieuChinh_ChiTiets[GetViewModel().Model.TSCD_DieuChinh_ChiTiets.Count - 1];
                            ChiTietChild.DienGiai = "Điều chỉnh giảm nguyên giá TSCĐ: " + item.Ma;
                            ChiTietChild.TKCoId = (int)item.TkNguyenGiaId;
                            ChiTietChild.SoTien = GetViewModel().Model.ChenhLechNguyenGia;
                        }

                    }
                    break;
                }
            }
          

        }
        void TSCD_DieuChinh_ThoiGianSuDungMoiChanged(object sender, EventArgs e)
        {

            System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)tscd_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();

            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.TSCDId)
                {
                    if (GetViewModel().Model.ThoiGianSuDungMoi < ThoiGianSuDungMoiHienThoi && ThoiGianSuDungMoiHienThoi != null)
                    {
                        int index = 0;
                        foreach (TSCD_DieuChinh_ChiTietChild childItem in GetViewModel().Model.TSCD_DieuChinh_ChiTiets)
                        {
                            if (childItem.DienGiai.Equals("Điều chỉnh tăng thời gian sử dụng TSCĐ: " + item.Ma))
                            {
                                this.DeleteDieuChinhDetailsItem(index);
                                break;
                            }
                            index++;
                        }
                        this.AddDieuChinhDetailsItem();
                        GetViewModel().FinishAddNew += new EventHandler(OnFinishAddNewWhenThoiGianSuDungMoiChanged);
                        
                        break;
                    }
                    if (GetViewModel().Model.ThoiGianSuDungMoi > ThoiGianSuDungMoiHienThoi && ThoiGianSuDungMoiHienThoi != null)
                    {
                        int index = 0;
                        foreach (TSCD_DieuChinh_ChiTietChild childItem in GetViewModel().Model.TSCD_DieuChinh_ChiTiets)
                        {
                            if (childItem.DienGiai.Equals("Điều chỉnh giảm thời gian sử dụng TSCĐ: " + item.Ma))
                            {
                                this.DeleteDieuChinhDetailsItem(index);
                                break;
                            }
                            index++;
                        }

                        this.AddDieuChinhDetailsItem();
                        GetViewModel().FinishAddNew += new EventHandler(OnFinishAddNewWhenThoiGianSuDungMoiChanged);

                        break;
                    }
                    else
                    {
                        int index = 0;
                        foreach (TSCD_DieuChinh_ChiTietChild childItem in GetViewModel().Model.TSCD_DieuChinh_ChiTiets)
                        {
                            if (childItem.DienGiai.Equals("Điều chỉnh giảm thời gian sử dụng TSCĐ: " + item.Ma) || childItem.DienGiai.Equals("Điều chỉnh tăng thời gian sử dụng TSCĐ: " + item.Ma))
                            {
                                this.DeleteDieuChinhDetailsItem(index);
                                break;
                            }
                            index++;
                        }

                    }
                
                }
            }
            
        }
        
        void OnFinishAddNewWhenThoiGianSuDungMoiChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)tscd_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();

            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.TSCDId)
                {
                    if (GetViewModel().Model.TSCD_DieuChinh_ChiTiets.Count != 0)
                    {
                        if (GetViewModel().Model.ThoiGianSuDungMoi < ThoiGianSuDungMoiHienThoi && ThoiGianSuDungMoiHienThoi != null)
                        {
                            TSCD_DieuChinh_ChiTietChild ChiTietChild = GetViewModel().Model.TSCD_DieuChinh_ChiTiets[GetViewModel().Model.TSCD_DieuChinh_ChiTiets.Count - 1];
                            ChiTietChild.DienGiai = "Điều chỉnh giảm thời gian sử dụng TSCĐ: " + item.Ma;
                            ChiTietChild.TKNoId = (int)item.TkChiPhiId;
                            ChiTietChild.TKCoId = (int)item.TkKhauHaoId;
                            if (ThoiGianSuDungMoiHienThoi != null)
                            {
                                ChiTietChild.SoTien = Math.Abs((double)((item.SoThangSD - ThoiGianSuDungMoiHienThoi) * (GetViewModel().Model.GiaTriKhauHaoThangMoi - item.GiaTriKhauHaoThang)));        
                            }
                        }
                        else if (GetViewModel().Model.ThoiGianSuDungMoi > ThoiGianSuDungMoiHienThoi && ThoiGianSuDungMoiHienThoi != null)
                        {
                            TSCD_DieuChinh_ChiTietChild ChiTietChild = GetViewModel().Model.TSCD_DieuChinh_ChiTiets[GetViewModel().Model.TSCD_DieuChinh_ChiTiets.Count - 1];
                            ChiTietChild.DienGiai = "Điều chỉnh tăng thời gian sử dụng TSCĐ: " + item.Ma;
                            ChiTietChild.TKCoId = (int)item.TkChiPhiId;
                            ChiTietChild.TKNoId = (int)item.TkKhauHaoId;
                            if (ThoiGianSuDungMoiHienThoi != null)
                            {
                                ChiTietChild.SoTien = Math.Abs((double)( (item.SoThangSD - ThoiGianSuDungMoiHienThoi) * (item.GiaTriKhauHaoThang - GetViewModel().Model.GiaTriKhauHaoThangMoi)));
                               
                            }
                        }
                    }
                    break;
                }
            }
            

        }

        void TSCD_DieuChinh_TSCDIdChanged(object sender, EventArgs e)
        {
                   
            System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)tscd_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();


            System.Windows.Data.CollectionViewSource tscd_KhauHao_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KhauHao_ListViewModelSource"];
            TSCD_KhauHao_ListViewModel tscd_KhauHao_ListViewModel = ((List<object>)tscd_KhauHao_CollectionViewSource.Source)[0] as TSCD_KhauHao_ListViewModel;
            System.Collections.Generic.IList<TSCD_KhauHaoInfo> listKhauHao = tscd_KhauHao_ListViewModel.Model.getListOfInfo();

            
            foreach (TSCD_KhauHaoInfo item in listKhauHao)
            {
                if (item.TSCDId == GetViewModel().Model.TSCDId)
                {
                    SoLanKhauHao = item.Count;
                    TongTienKhauHao = (double)item.Sum;
                    break;
                }
            }

            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.TSCDId)
                {
                    GetViewModel().Model.NguyenGiaMoi = item.NguyenGia;
                    GetViewModel().Model.ThoiGianSuDung = item.SoThangSD + 12 * item.SoNamSD;
                    GetViewModel().Model.ThoiGianSuDungMoi = GetViewModel().Model.ThoiGianSuDung - SoLanKhauHao;
                    GetViewModel().Model.GiaTriConLai = item.NguyenGia - TongTienKhauHao;
                    GetViewModel().Model.GiaTriConLaiMoi = GetViewModel().Model.NguyenGiaMoi - TongTienKhauHao;
                    ThoiGianSuDungMoiHienThoi = GetViewModel().Model.ThoiGianSuDungMoi;
                    GetViewModel().Model.GiaTriTinhKhauHaoMoi = item.GiaTriTinhKhauHao;
                    GetViewModel().Model.GiaTriKhauHaoThangMoi = item.GiaTriKhauHaoThang;                 
                    GetViewModel().Model.PhuongPhapTKHId = item.PhuongPhapTKHId;
                    break;
                }
            }
           

     
        }
     


        private void AddDieuChinhDetailsItem()
        {
            GetViewModel().AddTSCD_DieuChinh_ChiTietItem();
        }


        private void DeleteDieuChinhDetailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_DieuChinh_ChiTietItem( Index);
        }

       
        

        void TSCD_DieuChinh_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TSCD_DieuChinh_ChiTiets")
            {
                this.TSCD_DieuChinh_DieuChinhChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            
            
        }

       
        #endregion
        
    }
}
